package com.cloudkinto.service.order.impl;

import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudkinto.entity.OrderBigcommerceProductDo;
import com.cloudkinto.dao.OrderBigcommerceProductDao;
import com.cloudkinto.entity.OrderBigcommerceProductDo;
import com.cloudkinto.service.order.OrderBigcommerceProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.service.order.vo.bigcommerce.OrderBigComProductEntity;
import com.cloudkinto.service.order.vo.bigcommerce.OrderBigComProductEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ding
 * @since 2022-11-08
 */
@Service
public class OrderBigcommerceProductServiceImpl extends ServiceImpl<OrderBigcommerceProductDao, OrderBigcommerceProductDo> implements OrderBigcommerceProductService {
    @Autowired
    private OrderBigcommerceProductDao dao;

    @Override
    public void addEntityList(List<OrderBigComProductEntity> productEntities, Long id) {
        for (OrderBigComProductEntity orderBigComProductEntity : productEntities) {
            OrderBigcommerceProductDo orderBigcommerceProductDo = dao.selectOne(new LambdaQueryWrapper<OrderBigcommerceProductDo>()
                    .eq(OrderBigcommerceProductDo::getItemId, orderBigComProductEntity.getId())
                    .eq(OrderBigcommerceProductDo::getOrderId, id)
                    .last("limit 1"));
            if (orderBigcommerceProductDo==null){
                BeanUtils.copyProperties(orderBigComProductEntity,orderBigcommerceProductDo);
                orderBigcommerceProductDo.setItemId(orderBigComProductEntity.getId());
                dao.insert(orderBigcommerceProductDo);
            }else {
                Long id1 = orderBigcommerceProductDo.getId();
                BeanUtils.copyProperties(orderBigComProductEntity,orderBigcommerceProductDo);
                orderBigcommerceProductDo.setItemId(orderBigComProductEntity.getId());
                orderBigcommerceProductDo.setId(id1);
                dao.updateById(orderBigcommerceProductDo);
            }
        }
    }
}
